import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class LongestSubstrKDistinct340 {

    public static int lengthOfLongestSubstringKDistinct(String s, int k) {
        int windowStart = 0, maxLength = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int windowEnd = 0; windowEnd < s.length(); windowEnd++) {
            if (map.containsKey(s.charAt(windowEnd)))
                map.put(s.charAt(windowEnd), map.get(s.charAt(windowEnd)) + 1);
            else
                map.put(s.charAt(windowEnd), 1);
            while (map.size() > k) {
                map.put(s.charAt(windowStart), map.get(s.charAt(windowStart)) - 1);
                if (map.get(s.charAt(windowStart)) == 0)
                    map.remove(s.charAt(windowStart));
                windowStart++;
            }
            maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
        }
        return maxLength;
    }

    public static void main(String[] args) {
//        System.out.println(lengthOfLongestSubstringKDistinct("eceba",2));
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
        map.put("A", 1);
        map.put("B",2);
        map.put("C",3);
        map.put("D", 4);
        map.size();
        while(map.get("B") != null)
        map.remove(map.entrySet().iterator().next().getKey());
        for(Map.Entry e: map.entrySet())
            System.out.println(e);
        String s = "ABC";
        System.out.println(s.indexOf('A') == -1);
    }
}
